package org.fuys.coder.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fuys.coder.domain.act.lottery.model.vo.award.AwardVO;
import org.fuys.coder.domain.act.lottery.repository.ILotteryActAwardRepository;
import org.fuys.coder.infrastructure.dao.AwardMapper;
import org.fuys.coder.infrastructure.dao.LotteryAwardMapper;
import org.fuys.coder.infrastructure.model.po.Award;
import org.fuys.coder.infrastructure.model.po.LotteryAward;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.infrastructure.repository
 * @className: LotteryActAwardRepository
 * @author: WangJing
 * @description: 活动抽奖仓储实现
 * @date: 2024/7/8 23:35
 * @version: 1.0
 */
@Service
public class LotteryActAwardRepository extends ServiceImpl<LotteryAwardMapper, LotteryAward>
        implements ILotteryActAwardRepository {

    @Resource
    private LotteryAwardMapper lotteryAwardMapper;

    @Resource
    private AwardMapper awardMapper;

    @Override
    public void addLotteryAwardIds(List<Long> ids,Long lotteryId) {
        final List<LotteryAward> collect = ids.stream().map(id -> {
            LotteryAward lotteryAward = new LotteryAward();
            lotteryAward.setAwardId(id);
            lotteryAward.setActId(lotteryId);
            return lotteryAward;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

    @Override
    public List<AwardVO> getLotteryAwardsByActId(Long actId) {
        final List<LotteryAward> lotteryAwards = lotteryAwardMapper.selectList(new LambdaQueryWrapper<LotteryAward>()
                .select(LotteryAward::getAwardId)
                .eq(LotteryAward::getActId, actId));
        if(ObjectUtils.isEmpty(lotteryAwards)){
            return null;
        }
        final List<Long> awardIds = lotteryAwards.stream().map(LotteryAward::getAwardId).collect(Collectors.toList());
        final List<Award> awards = awardMapper.selectList(new LambdaQueryWrapper<Award>()
                .select(Award::getId,Award::getAwardName,Award::getAwardDesc,Award::getAwardCover,Award::getAwardType)
                .in(Award::getId, awardIds));
        if(ObjectUtils.isEmpty(awards)){
            return null;
        }
        return awards.stream().map(item -> {
            AwardVO awardVO = new AwardVO();
            awardVO.setAwardId(item.getId());
            awardVO.setAwardName(item.getAwardName());
            awardVO.setAwardDesc(item.getAwardDesc());
            awardVO.setAwardCover(item.getAwardCover());
            awardVO.setAwardType(item.getAwardType());
            return awardVO;
        }).collect(Collectors.toList());

    }
}
